trigger buildCode on Lead (before insert) {

    // generates a unique id for reference in public domain.
    for(Lead l : trigger.new){
        DateTime now = System.now();
        String formattednow = now.formatGmt('yyyy-MM-dd')+'T'+ now.formatGmt('HH:mm:ss')+'.'+now.formatGMT('SSS')+'Z';
        String canonical = UserInfo.getUserId() + l.LastName + l.email + l.firstName + formattednow;
        Blob bsig = Crypto.generateDigest('MD5', Blob.valueOf(canonical));
        String token = EncodingUtil.base64Encode(bsig);
        if(token.length() > 255){token=token.substring(0,254);}
        l.Unique_Id__c=Encodingutil.urlEncode(token,'UTF-8').replaceAll('%','_');
        }

}